// src/utils/localStorageManager.ts

// 设置本地存储项
export const setItem = (key: string, value: any) => {
  try {
    const serializedValue = JSON.stringify(value);
    localStorage.setItem(key, serializedValue);
  } catch (error) {
    console.error("设置本地存储项时出错:", error);
  }
};

// 获取本地存储项
export const getItem = (key: string) => {
  try {
    const serializedValue = localStorage.getItem(key);
    if (serializedValue === null) {
      return null;
    }
    return JSON.parse(serializedValue);
  } catch (error) {
    console.error("获取本地存储项时出错:", error);
    return null;
  }
};

// 删除本地存储项
export const removeItem = (key: string) => {
  try {
    localStorage.removeItem(key);
  } catch (error) {
    console.error("删除本地存储项时出错:", error);
  }
};

// 清空所有本地存储项
export const clearAll = () => {
  try {
    localStorage.clear();
  } catch (error) {
    console.error("清空本地存储时出错:", error);
  }
};

export const logout = () => {
  removeItem('token');
  removeItem('user');
  window.location.reload();
};
